package com.guaika.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guaika.reggie.common.BaseContext;
import com.guaika.reggie.common.ResultMessage;
import com.guaika.reggie.common.StringChars;
import com.guaika.reggie.entity.ShoppingCart;
import com.guaika.reggie.mapper.ShoppingCartMapper;
import com.guaika.reggie.service.ShoppingCartService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author GuaiKa
 */
@Service
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart> implements ShoppingCartService {
    @Override
    public ResultMessage<ShoppingCart> shoppingSave(ShoppingCart shoppingCart) {
        //获取当前登录用户id
        Long userId = BaseContext.get();
        shoppingCart.setUserId(userId);
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,userId);
        Long dishId = shoppingCart.getDishId();
        //判断添加的是套餐还是菜品
        if(dishId!=null){
            queryWrapper.eq(ShoppingCart::getDishId,dishId);
        }else{
            queryWrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }
        ShoppingCart cart = this.getOne(queryWrapper);
        //如果存在则在原有的份数上加1
        if(cart!=null){
            cart.setUserId(userId);
            cart.setNumber(cart.getNumber()+1);
            this.updateById(cart);
        }else {
            shoppingCart.setNumber(1);
            this.save(shoppingCart);
            cart = shoppingCart;
        }
        return ResultMessage.success(cart);
    }

    @Override
    public ResultMessage<List<ShoppingCart>> shoppingList() {
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,BaseContext.get());
        return ResultMessage.success(this.list(queryWrapper));
    }

    @Override
    public ResultMessage<ShoppingCart> shoppingSub(ShoppingCart shoppingCart) {
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,BaseContext.get());
        //判断是哪种类型的减少
        if(shoppingCart.getDishId()!=null){
            queryWrapper.eq(ShoppingCart::getDishId, shoppingCart.getDishId());
        }else {
            queryWrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }
        ShoppingCart cart = this.getOne(queryWrapper);
        cart.setNumber(cart.getNumber()-1);
        this.updateById(cart);
        return ResultMessage.success(cart);
    }

    @Override
    public ResultMessage<String> shoppingClean() {
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,BaseContext.get());
        this.remove(queryWrapper);
        return ResultMessage.success(StringChars.DELETE_CART_SUCCESS.getStr());
    }
}
